package com.example.admin.controller;

import com.example.admin.service.BlogService;
import com.example.xo.common.Result;
import com.example.xo.pojo.Blog;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("blog")
public class BlogController {

    @Autowired
    private BlogService blogService;

    /**
     * 增加
     */
    @PostMapping("add")
    public Result add(@RequestBody Blog blog) {
        //标题不能为空             .trim()去除两端空格（中间无法去除）
        if (blog.getTitle() == null || blog.getTitle().trim().length() == 0) {
            log.info("标题为空");
            return Result.error();
        }

        //标题不超过50个字符
        if (blog.getTitle().length() > 50) {
            log.info("标题长度过长");
            return Result.error();
        }

        //分类不能为空             .trim()去除两端空格（中间无法去除）
        if (blog.getCategoryId() == null || blog.getCategoryId().trim().length() == 0) {
            log.info("分类为空");
            return Result.error();
        }

        //作者不能为空                  .trim()去除两端空格（中间无法去除）
        if (blog.getAuthor() == null || blog.getAuthor().trim().length() == 0) {
            log.info("作者不能为空");
            return Result.error();
        }

        //作者不超过10个字符
        if (blog.getAuthor().length() > 10) {
            log.info("标题长度过长");
            return Result.error();
        }

        //内容不能为空             .trim()去除两端空格（中间无法去除）
        if (blog.getContent() == null || blog.getContent().trim().length() == 0) {
            log.info("内容为空");
            return Result.error();
        }

        return Result.success(blogService.add(blog));
    }


    /**
     * 删除
     */
    @DeleteMapping("deleteById/{id}")
    public Result delete(@PathVariable String id) {
        return Result.success(blogService.deleteById(id));
    }


    /**
     * 修改
     */
    @PutMapping("update")
    public Result update(@RequestBody Blog blog) {
        return Result.success(blogService.update(blog));
    }


    /**
     * 通过Id查询(VO)
     */

    @GetMapping("findById/{id}")
    public Result findById(@PathVariable String id) {
        return Result.success(blogService.findById(id));
    }

    /**
     * 通过Id查询(pojo)
     */

    @GetMapping("findBlogById/{id}")
    public Result findBlogById(@PathVariable String id) {
        return Result.success(blogService.findBlogById(id));
    }


    /**
     * 查询全部
     */
    @GetMapping("findAll")
    public Result findAll() {
        return Result.success(blogService.findAll());
    }


    /**
     * 查询blog全部
     */
    @GetMapping("findAllBlog")
    public Result findAllBlog() {
        return Result.success(blogService.findAllBlog());
    }

}
